package com.rich.group.myqueue;

import com.rich.common.constant.MyGlobalConstant;
import com.rich.group.mapper.GroupBuyMapper;
import com.rich.group.mapper.GroupJoinMapper;
import com.rich.group.pojo.GroupJoin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author minicoco
 * @version 1.0
 * @date 2021/9/5 20:51
 */
@Component
@Slf4j
public class JoinGroupQueue {

    @Autowired
    private GroupJoinMapper groupJoinMapper;
    @Autowired
    private GroupBuyMapper groupBuyMapper;


    /**
     * 拼团记录添加队列
     */
    @RabbitHandler
    @RabbitListener(queues = MyGlobalConstant.GroupBuyConstant.ADD_GROUP_RECORD)
    public void addRecordQueue(GroupJoin groupJoin){
        //添加参团记录
        groupJoinMapper.addJoin(groupJoin);
        //修改参团人数
        groupBuyMapper.updateJoinNum(groupJoin.getGroupId(),1);
    }

    /**
     * 修改拼团活动状态
     */
    @RabbitHandler
    @RabbitListener(queues = MyGlobalConstant.GroupBuyConstant.UPDATE_GROUP_STATUS)
    public void updateGroupStatus(Long groupId){
         groupBuyMapper.updateStatus(groupId, MyGlobalConstant.GroupBuyConstant.IS_END);
    }

}
